package com.crack.nga2.activity.userCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.activity.BaseActivity;
import com.crack.nga2.activity.FloorActivity;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.entity.Reply;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.TimeUtils;
import com.crack.nga2.utils.HttpParams;
import com.crack.nga2.volley.UserReplyRequest;

import java.util.ArrayList;
import java.util.List;

public class UserReplyActivity extends BaseActivity implements AdapterView.OnItemClickListener, Response.ErrorListener, Response.Listener<List<Reply>> {

    private PullListView listView;
    private ReplyAdapter adapter = new ReplyAdapter();
    private List<Reply> replies = new ArrayList<>();
    private int page;
    private RequestQueue mQueue;
    private int uid;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_reply);
        uid = getIntent().getIntExtra("uid", -1);
        title = $(R.id.action_title);
        mQueue = Volley.newRequestQueue(getApplicationContext());
        listView = (PullListView) findViewById(R.id.listView);
        listView.setNoPullUp(true);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        listView.onStartHeadRefresh();
        page = 1;
        getData();
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finish();
                break;
        }
    }


    protected String getPath() {
        return "thread.php?";
    }

    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        if (uid != -1) {
            params.put("authorid", uid);
            title.setText("他的回复");
        } else {
            params.put("authorid", GlobalConst.UID);
        }
        params.put("searchpost", 1);
        params.put("lite", "js");
        params.put("page", page);
        return params;
    }

    protected void getData() {
        String url = GlobalConst.BASE_URL + getPath() + getParams().toString();
        mQueue.add(new UserReplyRequest(Request.Method.GET, url, this, this));
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        listView.onCompleteRefresh();
    }

    @Override
    public void onResponse(List<Reply> response) {
        if (page == 1) {
            replies.clear();
        }
        replies.addAll(response);
        adapter.notifyDataSetChanged();
        listView.onCompleteRefresh();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, FloorActivity.class);
        intent.putExtra("theme", (int) id);
        startActivity(intent);
    }

    private class ReplyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return replies.size();
        }

        @Override
        public Object getItem(int position) {
            return replies.get(position);
        }

        @Override
        public long getItemId(int position) {
            return replies.get(position).tid;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            Reply reply = replies.get(position);
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(UserReplyActivity.this, R.layout.activity_user_reply_item, null);
                holder.subject = (TextView) view.findViewById(R.id.subject);
                holder.content = (TextView) view.findViewById(R.id.content);
                holder.lastPoster = (TextView) view.findViewById(R.id.last_poster);
                holder.lastPostTime = (TextView) view.findViewById(R.id.last_reply_time);
                holder.replyCount = (TextView) view.findViewById(R.id.reply_count);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.subject.setText(reply.subject);
            holder.lastPoster.setText(reply.lastReplier);
            holder.content.setText(reply.content);
            holder.replyCount.setText(String.valueOf(reply.replies));
            holder.lastPostTime.setText(TimeUtils.format(reply.lastReplyTime * 1000));
            return view;
        }
    }

    private class ViewHolder {
        TextView content;
        TextView subject;
        TextView lastPoster;
        TextView lastPostTime;
        TextView replyCount;
    }
}
